package CarEngine;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.light.PointLight;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.export.Savable;
import com.jme.util.export.binary.BinaryExporter;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.game.StandardGame;
import com.jmex.game.state.BasicGameState;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.MaxToJme;
import com.jmex.model.converters.ObjToJme;
import com.jmex.model.util.ModelLoader;
 
/**
 *
 * @author Gronau
 */
public class CubeGameState extends BasicGameState {
 
    private static String texture = "jmetest/data/images/Monkey.jpg";
    private static StandardGame standardGame;
    public static Spatial loadModel (String modelFile){
		Spatial			loadedModel	= null;
		FormatConverter		formatConverter = null;		
		ByteArrayOutputStream 	BO 		= new ByteArrayOutputStream();
		String			modelFormat 	= modelFile.substring(modelFile.lastIndexOf(".") + 1, modelFile.length());
		String			modelBinary	= modelFile.substring(0, modelFile.lastIndexOf(".") + 1) + "jbin";
		URL			modelURL	= ModelLoader.class.getClassLoader().getResource(modelBinary);
 
		//verify the presence of the jbin model
		if (modelURL == null){
 
			modelURL		= ModelLoader.class.getClassLoader().getResource(modelFile);
 
			//evaluate the format
			if (modelFormat.equals("3ds")){
				formatConverter = new MaxToJme();
			
			} else if (modelFormat.equals("obj")){
				formatConverter = new ObjToJme();
				formatConverter.setProperty("mtllib", modelURL);
			}
			//formatConverter.setProperty("mtllib", modelURL);
 
			try {
				formatConverter.convert(modelURL.openStream(), BO);
				loadedModel = (Spatial) BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
 
				//save the jbin format
				BinaryExporter.getInstance().save((Savable)loadedModel, new File(modelBinary));
			} catch (IOException e) {				
				e.printStackTrace();
				return null;
			}
		}else{
			try {
				//load the jbin format
				loadedModel = (Spatial) BinaryImporter.getInstance().load(modelURL.openStream());
			} catch (IOException e) {
				return null;
			}
		}
 
		return loadedModel;
	}
    
    public CubeGameState() {
        super("cubeGameState");
//        final Box box = new Box("MonkeyBox", new Vector3f(0, 0, 0), 5, 5, 5);
// 
//        //Material: gray
//        final MaterialState ms = DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
//        ms.setEmissive(new ColorRGBA(0.5f, 0.5f, 0.5f, 1));
//        box.setRenderState(ms);
// 
//        //Texture: the Monkey
//        final TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
//        final Texture t = TextureManager.loadTexture(
//                BasicGameState.class.getClassLoader().getResource(texture),
//                Texture.MinificationFilter.BilinearNoMipMaps,
//                Texture.MagnificationFilter.Bilinear);
//        ts.setTexture(t);
//        box.setRenderState(ts);
//        box.setModelBound(new BoundingBox());
//        box.updateModelBound();
//        Node boxNode = new Node("MonkeyBoxNode");
//        boxNode.attachChild(box);
        //getRootNode().attachChild(boxNode);
        Spatial map = loadModel("jmetest/data/model/racetrack/MountainValley_Track_Raceway.obj");
		map.setLocalScale(.1f);
		map.setLocalTranslation(10.1f, -0.54f, 6.0f);
		
		Node car = (Node)loadModel("jmetest/data/model/Car_Williams/f1car.jbin");//("jmetest/data/model/Grand/GPE.obj");
        car.setLocalScale(0.3f);
        
        CameraNode cn=new CameraNode("camera node",DisplaySystem.getDisplaySystem().getRenderer().getCamera());
      
        
        //car.getLocalRotation().fromAngles(0f, 180f, 0f);
		car.getLocalRotation().fromAngles(0f, FastMath.DEG_TO_RAD *180f, 0f);
		car.getLocalTranslation().addLocal(0.029f, -0.46f, 22.4f);
		car.attachChild(cn);
        cn.setLocalTranslation(0f, 1.3f, -5.5f);
        getRootNode().attachChild(map);
        getRootNode().attachChild(car);
		//Spot on!
        final PointLight light = new PointLight();
        light.setDiffuse(new ColorRGBA(0.75f, 0.75f, 0.75f, 0.75f));
        light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
        light.setLocation(new Vector3f(100, 100, 100));
        light.setEnabled(true);
 
        final LightState lightState = DisplaySystem.getDisplaySystem().getRenderer().createLightState();
        lightState.setEnabled(true);
        lightState.attach(light);
        getRootNode().setRenderState(lightState);
 
        getRootNode().updateRenderState();
        //Oh, and don't forget the controller...
        getRootNode().addController(new CubeController((Node)car));
    }
    
    
    
    public static void main(String[] args) {
        standardGame = new StandardGame("GameControl", StandardGame.GameType.GRAPHICAL, null);
        standardGame.start();
 
        GameState cubeState = new CubeGameState();
        GameStateManager.getInstance().attachChild(cubeState);
        cubeState.setActive(true);
    }
}